package org.onejava.core.tools;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class BaseIocTool {
	//private static DateFormat  sdfsimple=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟 
	//private static  DateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//小写的mm表示的是分钟  

	public static Object setBean(Map<String,String> map,Class<?> beanClass) throws Exception{
		Field[] fieldList=beanClass.getDeclaredFields();
		Map<String,Field> setMethodMap = new HashMap<String, Field>();
		Object obj = ObjectTools.getTheObject(beanClass);

		for(int i=0;i<fieldList.length;i++){
			String value = map.get(fieldList[i].getName());
			 if(null!=value){
				 fieldList[i].setAccessible(true);
				 fieldList[i].set(obj, changeType(fieldList[i].getType(),value));
			 }
		}
		
		return obj;
		
	}
	
     public static Object changeType(Class<?> classType,String value) throws Exception{
		//Object obj =null;
		if(classType.equals(String.class)){
			 return value;	
		}else
		if(classType.equals(int.class)){
			 int obj=Integer.parseInt(value);
			 return obj;
		}else
		if(classType.equals(Integer.class)){
			 int obj=Integer.parseInt(value);
			 return obj;
		}else
		if(classType.equals(Double.class)){
			double obj=Double.valueOf(value).doubleValue();
			return obj;
		}else
		if(classType.equals(Long.class)){
			long obj = Long.parseLong(value);
			return obj;
		}else
		if(classType.equals(Date.class)){
			Date obj =null;
			if(value.length()<=11){
				DateFormat sdfsimple=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟  
				obj=sdfsimple.parse(value);
			}else{
				DateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//小写的mm表示的是分钟
				obj=sdf.parse(value);
			}
			return obj;
		}
		return value;
	}
	
	public static Object setBean2(Map<String,Object> map,Class<?> beanClass) throws Exception{
		Method[]  methods =beanClass.getMethods();
		//把符合注入规则(setXxx)的方法提取出来
		Map<String,Method> setMethodMap = new HashMap<String, Method>();
		for(int j=0;j<methods.length;j++){
			String methodName = methods[j].getName();
			if(methodName.indexOf("set")==0){
				setMethodMap.put(methodName, methods[j]);
			}
		}
		Object obj = ObjectTools.getTheObject(beanClass);
		//遍历参数
		Iterator<String> iter = map.keySet().iterator();
		while (iter.hasNext()) {
			String name = iter.next();
			//把参数转换成setXxx的格式，并找到与之对应的方法
			String name2="set"+(new StringBuilder()).append(Character.toUpperCase(name.charAt(0))).append(name.substring(1)).toString();
			Method method =setMethodMap.get(name2);
			//在实体类中注入参数
			if(null!=method){
				ObjectTools.invokeObject(obj, method,changeType(method,map.get(name)) );			
			}
		}
		return obj;
	}
	
	/**
	 * 扩展方法，用于调整参数的类型
	 * @param method
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	private static Object changeType(Method method,Object obj) throws Exception{
		Class<?>[] Class = method.getParameterTypes();
		
		return obj;
	}
	
}
